<?php

namespace app\modules\web\controllers;

use yii\web\Controller;
use \app\modules\web\controllers\common\BaseController;
use \app\models\brand\BrandSetting;
use \app\models\brand\BrandImages;

class BrandController extends BaseController
{
    //品牌信息
    public function actionInfo()
    {
    	$this->layout="main";
        $info=BrandSetting::find()->one();
        return $this->render('info',['info'=>$info]);
    }
    //品牌设置
    public function actionSet()
    {
    	$this->layout="main";
        if(\Yii::$app->request->isGet){
            $info=BrandSetting::find()->one();

            return $this->render('set',['info'=>$info]);
        }
        $image_key=trim($this->post("image_key",""));
        
        $name=trim($this->post("name","")) ;
        $mobile=trim($this->post("mobile",""));
        $address=trim($this->post("address",""));
        $description=trim($this->post("description",""));
        //添加品牌或更新品牌
        $info=BrandSetting::find()->one();
        if($info){
            $new_brand=$info;
        }else{
            $new_brand=new BrandSetting();
            $new_brand->created_time=$now_date;
        }
        $new_brand->name=$name;
        $new_brand->mobile=$mobile;
        $new_brand->address=$address;
        $new_brand->description=$description;
        $new_brand->updated_time=time();
        $new_brand->logo=$image_key;
        $new_brand->save(0);
        return $this->renderJson([],"操作成功");
        
    }
    //品牌图片

    public function actionImg()
    {
    	$this->layout="main";
        $info=BrandImages::find()->all();

        return $this->render('Img',['info'=>$info]);
    }
    public function actionSetImg()
    {
        $image_key=$this->post("image_key","");
        
        $info=BrandImages::find()->where(['image_key'=>$image_key])->one();
        $count=BrandImages::find()->count();
        if($info){
            return $this->renderJson([],'已经上传过了!');
        }
        if($count>5){
            return $this->renderJson([],'最多上传五张!');
        }
        $new_brand=new BrandImages();
        $new_brand->image_key=$image_key;
        $new_brand->created_time=time();
        $new_brand->save(0);
        return $this->renderJson([],'上传成功~');
    }
    public function actionDelImg(){
        $id=$this->post("id","");
        $res=BrandImages::findOne($id)->delete();
        if(!$res){
            return $this->renderJson([],'删除失败~');
        }
        return $this->renderJson([],'删除成功~');
    }
}
